package com.yj.jason.jsondemo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;

import com.yj.jason.baselibrary.base.BaseActivity;
import com.yj.jason.baselibrary.view.indicator.IndicatorAdapter;
import com.yj.jason.baselibrary.view.indicator.IndicatorLayout;
import com.yj.jason.jsondemo.fragment.Fragment01;
import com.yj.jason.jsondemo.fragment.Fragment02;
import com.yj.jason.jsondemo.fragment.Fragment03;
import com.yj.jason.jsondemo.fragment.Fragment04;

import java.util.ArrayList;
import java.util.List;

public class FragmentLoadActivity extends BaseActivity{

    private ViewPager vp;
    private IndicatorLayout id_layout;

    private String[] mTabName = {"全部", "待审核", "未通过", "已通过","分电视剧","二套房","二分","封建时代","附近的","发发"};
  //  private String[] mTabName = {"全部", "待审核","未通过", "已通过已通过"};
    private List<Fragment> mFragmentList = new ArrayList<Fragment>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_load);

        vp=(ViewPager)findViewById(R.id.vp);
        id_layout=(IndicatorLayout)findViewById(R.id.id_layout);


        for(int i=0;i<mTabName.length;i++){
            Fragment01 fragment01=new Fragment01(mTabName[i]);
            mFragmentList.add(fragment01);
        }

        vp.setAdapter(new CommomFragmentPagerAdapter(getSupportFragmentManager(),mFragmentList,mTabName));


        id_layout.setAdapter(new IndicatorAdapter() {
            @Override
            public int getCount() {
                return mTabName.length;
            }

            @Override
            public View getView(ViewGroup parent, int position) {
                TextView textView=new TextView(FragmentLoadActivity.this);
                textView.setText(mTabName[position]);
                textView.setPadding(10,10,10,10);
                textView.setTextColor(Color.BLACK);
                textView.setGravity(Gravity.CENTER);
                return textView;
            }

            @Override
            public void highLightIndicator(View view) {
                TextView tv= (TextView) view;
                tv.setTextColor(Color.RED);
            }

            @Override
            public void restoreIndicator(View view) {
                TextView tv= (TextView) view;
                tv.setTextColor(Color.BLACK);
            }


            @Override
            public View getBottomTrackView() {
                View view=new View(FragmentLoadActivity.this);
                view.setBackgroundColor(Color.RED);
                view.setLayoutParams(new ViewGroup.LayoutParams(30,8));
                return view;
            }
        },vp);



    }

    @Override
    public void initData() {

    }


    class CommomFragmentPagerAdapter extends FragmentPagerAdapter {
        List<Fragment> fragmentList = new ArrayList<Fragment>();
        private String[] mTitles;

        public CommomFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        public CommomFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, String[] mTitles) {
            super(fm);
            this.fragmentList = fragmentList;
            this.mTitles = mTitles;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if(mTitles==null)return "";
            return mTitles.length>0 ? mTitles[position] : "";
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }



    }

}
